//
//  NSDictionary+Additions.m
//  HSCategoryKit
//
//  Created by huang on 2018/8/1.
//

#import "NSDictionary+Additions.h"

@implementation NSDictionary (Additions)

#pragma mark 获取bool类型值
- (BOOL)getBoolValueForKey:(NSString *)key defaultValue:(BOOL)defaultValue
{
    id tmpValue = [self objectForKey:key];
    
    if (tmpValue == nil || tmpValue == [NSNull null]) {
        return defaultValue;
    }
    
    if ([tmpValue isKindOfClass:[NSNumber class]]) {
        return [tmpValue boolValue];
    } else {
        @try {
            return [tmpValue boolValue];
        }
        @catch (NSException *exception) {
            return defaultValue;
        }
    }
}

#pragma mark 获取NSInteger类型值
- (NSInteger)getIntValueForKey:(NSString *)key defaultValue:(NSInteger)defaultValue
{
    id tmpValue = [self objectForKey:key];
    
    if (tmpValue == nil || tmpValue == [NSNull null]) {
        return defaultValue;
    }
    
    if ([tmpValue isKindOfClass:[NSNumber class]]) {
        return [tmpValue intValue];
    } else {
        @try {
            return [tmpValue intValue];
        }
        @catch (NSException *exception) {
            return defaultValue;
        }
    }
}

#pragma mark 获取CGFloat类型值
- (CGFloat)getFloatValueForKey:(NSString *)key defaultValue:(CGFloat)defaultValue
{
    id tmpValue = [self objectForKey:key];
    
    if (tmpValue == nil || tmpValue == [NSNull null]) {
        return defaultValue;
    }
    
    if ([tmpValue isKindOfClass:[NSNumber class]]) {
        return [tmpValue floatValue];
    } else {
        @try {
            return [tmpValue floatValue];
        }
        @catch (NSException *exception) {
            return defaultValue;
        }
    }
}

#pragma mark 获取double类型值
- (double)getDoubleValueForKey:(NSString*)key defaultValue:(double)defaultValue
{
    id tmpValue = [self objectForKey:key];
    
    if (tmpValue == nil || tmpValue == [NSNull null]) {
        return defaultValue;
    }
    
    if ([tmpValue isKindOfClass:[NSNumber class]]) {
        return [tmpValue doubleValue];
    } else {
        @try {
            return [tmpValue doubleValue];
        }
        @catch (NSException *exception) {
            return defaultValue;
        }
    }
}

#pragma mark 获取long long类型值
- (long long)getLongLongValueValueForKey:(NSString *)key defaultValue:(long long)defaultValue
{
    id tmpValue = [self objectForKey:key];
    
    if (tmpValue == nil || tmpValue == [NSNull null]) {
        return defaultValue;
    }
    
    if ([tmpValue isKindOfClass:[NSNumber class]]) {
        return [tmpValue longLongValue];
    } else {
        @try {
            return [tmpValue longLongValue];
        }
        @catch (NSException *exception) {
            return defaultValue;
        }
    }
}

#pragma mark 获取NSString类型值
- (NSString *)getStringValueForKey:(NSString *)key defaultValue:(NSString *)defaultValue
{
    id tmpValue = [self objectForKey:key];
    
    if (tmpValue == nil || tmpValue == [NSNull null]) {
        return defaultValue;
    }
    
    if ([tmpValue isKindOfClass:[NSString class]]) {
        return [NSString stringWithString:tmpValue];
    } else {
        @try {
            return [NSString stringWithFormat:@"%@",tmpValue];
        }
        @catch (NSException *exception) {
            return defaultValue;
        }
    }
}

#pragma mark 获取NSDictionary类型值
- (NSDictionary *)getDictionaryForKey:(NSString*)key
{
    id tmpValue = [self objectForKey:key];
    if ([tmpValue isKindOfClass:[NSDictionary class]]) {
        return tmpValue;
    }else if ([tmpValue isKindOfClass:[NSString class]]){
        NSData *tmpData = [tmpValue dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *uD = [NSJSONSerialization JSONObjectWithData:tmpData options:NSJSONReadingMutableContainers error:nil];
        return uD;
    }else {
        return nil;
    }
}

#pragma mark 获取NSArray类型值
- (NSArray *)getArrayForKey:(NSString*)key
{
    id tmpValue = [self objectForKey:key];
    if ([tmpValue isKindOfClass:[NSArray class]]) {
        return tmpValue;
    } else {
        return nil;
    }
}

@end
